package www.nearby.ren;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import www.nearby.ren.view.GlideImageLoader;
import www.nearby.ren.view.PileLayout;
import www.nearby.ren.view.viewcard.CardFragmentPagerAdapter;
import www.nearby.ren.view.viewcard.CardItem;
import www.nearby.ren.view.viewcard.CardPagerAdapter;
import www.nearby.ren.view.viewcard.ShadowTransformer;


/**
 * Created by Administrator on 2017/7/27 0027.
 */

public class NearbyFragmentHome extends Fragment {

    public PileLayout pileLayout;
    public Banner banner;
    ArrayList<String> dataList = new ArrayList<>();
    ViewPager mViewPager, mViewPager2;
    private CardPagerAdapter mCardAdapter,mCardAdapter2;
    private CardFragmentPagerAdapter mFragmentCardAdapter;
    private ShadowTransformer mCardShadowTransformer,mCardShadowTransformer2;
    private ShadowTransformer mFragmentCardShadowTransformer,mFragmentCardShadowTransformer2;

    public static float dpToPixels(int dp, Context context) {
        return dp * (context.getResources().getDisplayMetrics().density);
    }

    @Override
    public void onStart() {
        super.onStart();
        //结束轮播
        banner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
    }

    private List<String> adList = new ArrayList<>();

    public void initbanner(View view) {
        adList.add("http://img-cdn2.luoo.net/site/201707/5976f2d7b064f.jpg");
        adList.add("http://img-cdn2.luoo.net/site/201707/59771edc928c3.jpg");
        adList.add("http://img-cdn2.luoo.net/site/201707/5976eceddaac3.jpg");
        adList.add("http://img-cdn2.luoo.net/site/201707/5968a76cedb6b.jpg");
        banner = (Banner) view.findViewById(R.id.banner);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setViewPagerIsScroll(true);
        banner.setBannerAnimation(Transformer.Default);
        banner.isAutoPlay(true);
        banner.setImageLoader(new GlideImageLoader());
        banner.setDelayTime(3000);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setImages(adList).start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
//                bannerSwitch(position);
            }
        });
    }

    public void initpager(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        mViewPager2 = (ViewPager) view.findViewById(R.id.viewPager2);
        mCardAdapter = new CardPagerAdapter();
        mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1));


        mCardAdapter2 = new CardPagerAdapter();
        mCardAdapter2.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter2.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter2.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mCardAdapter2.addCardItem(new CardItem(R.string.title_1, R.string.text_1));
        mFragmentCardAdapter = new CardFragmentPagerAdapter(getActivity().getSupportFragmentManager(),
                dpToPixels(2, getActivity()));

        mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter);
        mFragmentCardShadowTransformer = new ShadowTransformer(mViewPager, mFragmentCardAdapter);

        mCardShadowTransformer2 = new ShadowTransformer(mViewPager2, mCardAdapter2);
        mFragmentCardShadowTransformer2 = new ShadowTransformer(mViewPager2, mFragmentCardAdapter);

        mViewPager.setAdapter(mCardAdapter);
        mViewPager.setPageTransformer(false, mCardShadowTransformer);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager2.setAdapter(mCardAdapter2);
        mViewPager2.setPageTransformer(false, mCardShadowTransformer2);
        mViewPager2.setOffscreenPageLimit(3);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.neraby_fragment_home, container, false);
        initbanner(view);
        initpager(view);


//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        dataList.add("1");
//        initPle(view);
        return view;
    }

    public void initPle(View view) {
        pileLayout = (PileLayout) view.findViewById(R.id.pileLayout);
        pileLayout.setAdapter(new PileLayout.Adapter() {
            @Override
            public int getLayoutId() {
                // item's layout resource id
                return R.layout.item_layout;
            }

            @Override
            public void bindView(View view, int position) {
                ViewHolder viewHolder = (ViewHolder) view.getTag();
                if (viewHolder == null) {
                    viewHolder = new ViewHolder();
                    viewHolder.imageView = (ImageView) view.findViewById(R.id.imageView);
                    view.setTag(viewHolder);
                }
                // recycled view bind new position
            }

            @Override
            public int getItemCount() {
                // item count
                return dataList.size();
            }

            @Override
            public void displaying(int position) {
                // right displaying the left biggest itemView's position
            }

            @Override
            public void onItemClick(View view, int position) {
                // on item click
            }
        });

    }

    class ViewHolder {
        ImageView imageView;
    }
}
